<?php

/**
 * @content: 获取巡查记录数据，压缩成json格式数据
 * @Author:  Groube
 * @Date:    2017-03-09 17:40:00
 * @version  $Id$
 */
class GetxunchalistjsonController extends Yaf_Controller_Abstract {

    /**
     * @Title  禁用模板功能
     * @Author Groube
     * @Param  /
     * @Return /
     * @Date 2017/03/08 14:30
     */
    public function init() {
        $yiyuan_id = isset($_REQUEST['yiyuan_id']) ? $_REQUEST['yiyuan_id'] : "";

        if($yiyuan_id === "" ){
            Utility::apiJsonError(20028, '请输入医院ID');
        }elseif(!ctype_digit("$yiyuan_id")){
            Utility::apiJsonError(20027, '医院ID必须是整型');
        }

        Yaf_Dispatcher::getInstance()->disableView();
    }


    /**
     * @Title  入口方法
     * @Author Groube
     * @Param  /
     * @Return /
     * @Date 2017/03/09 17:40
     */
    function indexAction(){
        $request = $this->getRequest();
        $user_department = $request->getPost('user_department','');
        $user_department_id = $request->getPost('user_department_id','');
        $str_yiyuan_id = $request->getPost('yiyuan_id',0);
        $yiyuan_id = (int)$str_yiyuan_id;

        if(empty($user_department)){
            Utility::apiJsonError(40016,'(Method:获取巡查记录数据)科室不能为空!');
        }

        $async_result = $this->getXunchaCommon($user_department_id,$user_department,$yiyuan_id);

        if(empty($async_result)){
            $data = array('error'=>200,'msg'=>'暂无数据!','update_sql' => '');
            Utility::apiJsonResultExtends($data);
        }

        $params_mod = new ZhuyuanXunchaJiluModel;
        $field_arr = $params_mod->_field_arr;
        $ids = array();
        foreach ($async_result as $key=>$item)
        {
            $ids[$key] = $item['id'];
        }
        $detail_data = $params_mod->getXunchaJiluDetail($ids);
        $temp_res = Utility::formatToSqlStr($detail_data, 'zhuyuan_xuncha_jilu_detail', 'update_sql', ['id','xuncha_jilu_id','xuncha_item_code','xuncha_item_name','xuncha_item_type','modify_time','modify_time']);

        $async_result = Utility::formatToSqlStr($async_result, 'zhuyuan_xuncha_jilu', 'update_sql', $field_arr);
        //print_r($async_result);exit;
        $async_result['update_sql'][1] .= $temp_res['update_sql'][0];
        $async_result['error'] = 200;
        $async_result['msg'] = '';
        Utility::apiJsonResultExtends($async_result);
    }


    public function getXunchaCommon($user_department_id,$user_department,$yiyuan_id){
        if(Yaconf::get("config$yiyuan_id.fenbiao_state") == 'true'){
            //分表机制开启
            $zhuyuan_xuncha_jilu = "zhuyuan_xuncha_jilu_".$user_department_id;
        }else{
            $zhuyuan_xuncha_jilu = "zhuyuan_xuncha_jilu";
        }
        $zhuyuan_basic_info = DB::select('zhuyuan_id')->from('zhuyuan_basic_info')
            ->where('zhuyuan_bingqu','=',$user_department)
            ->where('zhuangtai','=','住院中')
            ->where('yiyuan_id','=',$yiyuan_id)
            ->execute()
            ->as_array();

        $zhuayun_bingren = array();
        if(!empty($zhuyuan_basic_info)){
            foreach($zhuyuan_basic_info as $one){
                if(!in_array($one['zhuyuan_id'],$zhuayun_bingren)){
                    $zhuayun_bingren[] = $one['zhuyuan_id'];
                }
            }
        }else{
            Utility::apiJsonError(40017,'(Method:获取巡查记录数据)住院基本信息表中没有查到数据!');
        }

        $params_mod = new ZhuyuanXunchaJiluModel;
        return $params_mod->ls($zhuyuan_xuncha_jilu,$zhuayun_bingren,$yiyuan_id);
    }
}